package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"time"
)

// CreateNewSortApiGauge 创建新的仪表盘 GaugeVec多个监控指标集合 数值可增减，上下波动
func CreateNewSortApiGauge(namespace string) *prometheus.GaugeVec {
	return prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: namespace,
		Name: "Http_api_duration",
		Help: "The total time of api,ms.",
	},[]string{"WSortAPi"})
}
// NewApiDurationGauge 生成新的自定义结构体 ApiDurationGauge
func NewApiDurationGauge(agauge *prometheus.GaugeVec,amethod string) *ApiDurationGauge {
	startTime := time.Now()
	return &ApiDurationGauge{
		gauge: agauge,
		start: startTime,
		method: amethod,
	}
}

// ObserveApiDuration method 访问耗时
func (r *ApiDurationGauge)ObserveApiDuration()  {
	end:=time.Now()
	(r.gauge).WithLabelValues(r.method).Set(end.Sub(r.start).Seconds())
}

type ApiDurationGauge struct {
	gauge *prometheus.GaugeVec
	start time.Time
	last time.Time
	method string
}